package com.qf.mybatisplus.service.impl;

import com.alibaba.fastjson.JSON;
import com.aliyuncs.CommonRequest;
import com.aliyuncs.CommonResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;

import com.qf.mybatisplus.service.SmsService;
import org.springframework.stereotype.Service;

import java.util.HashMap;

@Service
public class SmsServiceImpl implements SmsService {
    @Override
    public Boolean getPhoneCode(String phone) {
        DefaultProfile profile = DefaultProfile.getProfile("cn-hangzhou", "LTAI5tDedhVjd4WEEteksTRy", "bt7u3lG0akl4OirS2tRd0Nnw4McGxw");
        IAcsClient client = new DefaultAcsClient(profile);

        CommonRequest request = new CommonRequest();
        request.setSysMethod(MethodType.POST);
        request.setSysDomain("dysmsapi.aliyuncs.com");
        request.setSysVersion("2017-05-25");
        request.setSysAction("SendSms");
        request.putQueryParameter("PhoneNumbers", phone);
        // 签名
        request.putQueryParameter("SignName", "ABC商城");
        // 模板
        request.putQueryParameter("TemplateCode", "SMS_200702765");

        HashMap<String, String> map = new HashMap<>();

        // 随机生成验证码，存入redis，设置过期时间，可用于进行校验验证码
        map.put("code","123456");

        request.putQueryParameter("TemplateParam", JSON.toJSONString(map));
        try {
            CommonResponse response = client.getCommonResponse(request);
            System.out.println(response.getData());
            boolean success = response.getHttpResponse().isSuccess();
            if (success){
                return true;
            }
        } catch (ClientException e) {
            e.printStackTrace();
        }

        return false;
    }







}
